package ModelLayer;
import java.util.ArrayList;

/**
 * Write a description of class Stock here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SupplierCont
{
    private ArrayList<Supplier> sup;
    public static SupplierCont instance;
    
    public SupplierCont()
    {
        sup = new ArrayList<Supplier>();
    }

    
    public static SupplierCont getSingelton()
    {
        if(instance == null)
        {
            instance = new SupplierCont();
        }
        return instance;
    }
    
    public void createSupplier(Supplier supplier)
    {
        sup.add(supplier);
    }
    
    public Supplier findSupplier(int EaNr)
    {
        int index = 0;
        boolean found = false;
        while(index < sup.size() && !found)
        {
            if(sup.get(index).getEaNr() == EaNr)
            {
                found = true;
            }
            else
            {
                index++;
            }
        }
        return sup.get(index);
    }
    
    public void updateSupplier(String companyname, String city, String address, int postalcode, int eanr)
    {
        {
        int index = 0;
        boolean found = false;
        String stringPostalCode = Integer.toString(postalcode);
        String stringEaNr = Integer.toString(eanr);
        while(index < sup.size() && !found)
        {
            if(sup.get(index).getEaNr() == eanr)
            {
                if(companyname.length() > 0)
                {
                    sup.get(index).setCompanyName(companyname);
                }
                
                if(city.length() > 0)
                {
                    sup.get(index).setCity(city);
                }
                
                if(address.length() > 0)
                {
                    sup.get(index).setAddress(address); 
                }
                
                if(stringPostalCode.length() > 0)
                {
                    sup.get(index).setPostalCode(postalcode);
                }
                
                if(stringEaNr.length() > 0)
                {
                    sup.get(index).setEaNr(eanr);
                }
                found = true;
            }
            else
            {
                index++;
            }
        }
    }
}
    
    public void deleteSupplier(int eanr)
    {
        int index = 0;
        boolean found = false;
        while(index < sup.size() && !found)
        {
            if(sup.get(index).getEaNr() == eanr)
            {
                sup.remove(index);
                found = true;
            }
            else
            {
                index++;
            }
        }
    }
}
